#!/bin/sh
# PCP QA Test No. 1574
# Checking memory leaks for pmseries functions
#
# Copyright (c) 2022 Shiyao Chen.  All Rights Reserved.
#

seq=`basename $0`
echo "QA output created by $seq"
path=""

# get standard environment, filters and checks
. ./common.product
. ./common.filter
. ./common.check
. ./common.keys

# This test is not run if we dont have pmseries and a key server installed.
_check_series

# ditto for valgrind
_check_valgrind

_cleanup()
{
    [ -n "$key_server_port" ] && $keys_cli -p $key_server_port shutdown
    _restore_config $PCP_SYSCONF_DIR/pmseries
    cd $here
    $sudo rm -rf $tmp $tmp.*
}

status=1	# failure is the default!
trap "_cleanup; exit \$status" 0 1 2 3 15

_filter_load()
{
    sed \
	-e "s,$here,PATH,g" \
    #end
}

_filter_query()
{
    sed \
	-e "s,$key_server_port,KEYSERVERPORT,g" \
    #end
}

_sort_context()
{
    sed -e 's/    Context: //g' -e 's/, /\n/g' | \
    LC_COLLATE=POSIX sort | \
    $PCP_AWK_PROG 'BEGIN { printf "    Context: " } {
	if (count == 0) {printf("%s", $0)} else {printf(", %s", $0)};
	count++
    } END { print out }'
}

# real QA test starts here
key_server_port=`_find_free_port`
_save_config $PCP_SYSCONF_DIR/pmseries
$sudo rm -f $PCP_SYSCONF_DIR/pmseries/*

echo "Start test key server ..."
$key_server --port $key_server_port --save "" > $tmp.keys 2>&1 &
_check_key_server_ping $key_server_port
_check_key_server $key_server_port
echo

_check_key_server_version $key_server_port

args="-p $key_server_port -Z UTC"
query="kernel.all.load[count:2]"

echo "== Load metric data into this key server instance"
pmseries $args --load "{source.path: \"$here/archives/proc\"}" | _filter_load

echo
echo "== Running valgrind on stdev_sample() and stdev_inst()"
_run_valgrind pmseries $args "stdev_sample($query)" | _filter_query
_run_valgrind pmseries $args "stdev_inst($query)" | _filter_query

echo
echo "== Running valgrind on max_sample() and max_inst()"
_run_valgrind pmseries $args "max_sample($query)" | _filter_query
_run_valgrind pmseries $args "max_inst($query)" | _filter_query

echo
echo "== Running valgrind on min_sample() and min_inst()"
_run_valgrind pmseries $args "min_sample($query)" | _filter_query
_run_valgrind pmseries $args "min_inst($query)" | _filter_query

echo
echo "== Running valgrind on sum_sample() and sum_inst()"
_run_valgrind pmseries $args "sum_sample($query)" | _filter_query
_run_valgrind pmseries $args "sum_inst($query)" | _filter_query

echo
echo "== Running valgrind on avg_sample() and avg_inst()"
_run_valgrind pmseries $args "avg_sample($query)" | _filter_query
_run_valgrind pmseries $args "avg_inst($query)" | _filter_query

echo
echo "== Running valgrind with scalar multiplication"
_run_valgrind pmseries $args "avg_sample($query)" | _filter_query
_run_valgrind pmseries $args "avg_inst($query)" | _filter_query

echo "== Running valgrind with scalar multiplication"
_run_valgrind pmseries $args "kernel.all.uptime[count:2] * 2" | _filter_query

echo "== Running valgrind with function multiplication"
_run_valgrind pmseries $args "max(kernel.all.uptime[count:2]) * max(kernel.all.uptime[count:2])" | _filter_query

# all done
status=0
exit
